﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLShoppingCart
    {
        public bool AddGoodsToShoppingCart(int studentId, int goodsId, int goodsAmount)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                try
                {
                    var query = db.ShoppingCarts.FirstOrDefault(s => s.Student.Students_Id == studentId && s.Good.Goods_Id == goodsId);
                    if (query != null)
                    {
                        query.Goods_Amount += goodsAmount;
                    }
                    else
                    {
                        ShoppingCart S = new ShoppingCart();
                        S.Student = db.Students.SingleOrDefault(s => s.Students_Id == studentId);
                        S.Good = db.Goods.SingleOrDefault(g => g.Goods_Id == goodsId);
                        S.Goods_Amount = goodsAmount;
                        db.ShoppingCarts.InsertOnSubmit(S);
                    }
                    db.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;

                }
            }
        }

        public List<MShoppingCart> GetShoppingCartByStudentId(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.ShoppingCarts.Where(s => s.Students_Id.HasValue && s.Students_Id == studentId)
                                            .Select(s => new MShoppingCart
                                              {
                                                  Id = s.ID,
                                                  StudentId = s.Students_Id.Value,
                                                  GoodsName = s.Good.Goods_Name,
                                                  GoodsId = s.Good.Goods_Id,
                                                  GoodsAmount = s.Goods_Amount.HasValue ? s.Goods_Amount.Value : 0
                                              });
                return query.ToList();
            }
        }

        public bool DeleteGoodsFromShoppingCart(int studentId, int goodsId)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var sc = db.ShoppingCarts.Where(s => s.Student.Students_Id == studentId && s.Good.Goods_Id == goodsId);
                    db.ShoppingCarts.DeleteAllOnSubmit(sc);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }


        public bool DeleteGoodsListFromShoppingCart(int studentId, string goodsList)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var ary = goodsList.Split(',').Select(x => int.Parse(x));
                    var sc = db.ShoppingCarts.Where(s => s.Student.Students_Id == studentId && ary.Contains(s.Good.Goods_Id));
                    db.ShoppingCarts.DeleteAllOnSubmit(sc);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }

        public bool DeleteGoodsListFromShoppingCart(string shoppingCartIdList)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var ary = shoppingCartIdList.Split(',').Select(x => int.Parse(x));
                    var sc = db.ShoppingCarts.Where(s => ary.Contains(s.ID));
                    db.ShoppingCarts.DeleteAllOnSubmit(sc);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }


        public bool EditShoppingCart(int shoppingCartId, int ammount)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var sc = db.ShoppingCarts.Single(s => s.ID == shoppingCartId);
                    sc.Goods_Amount = ammount;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }
    }
}